/**
 * 
 */
package com.vanstone.imgapp.common.serial;

import java.io.ByteArrayOutputStream;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.pool2.impl.GenericObjectPool;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Output;
import com.vanstone.common.component.id.IDFactory;
import com.vanstone.imgapp.common.serial.object.CmsUser;

/**
 * @author shipeng <br />
 */
public class KryoFactoryMainApp {

  public static void main(String[] args) throws Exception {

    final GenericObjectPool<Kryo> genericObjectPool = new GenericObjectPool<>(new KryoFactory());

    ExecutorService executorService = Executors.newFixedThreadPool(100);
    for (int i = 0; i < 100; i++) {
      executorService.submit(new Runnable() {
        @Override
        public void run() {
          for (int n = 0; n < 100000; n++) {
            CmsUser user = new CmsUser();
            user.setId(IDFactory.generateID());
            user.setUsername("shipeng");
            user.setPassword(RandomStringUtils.random(4));
            user.setSysInsertTime(new Date());
            Kryo kryo;
            try {
              kryo = genericObjectPool.borrowObject();
              System.out.println(kryo.toString());
              ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
              Output output = new Output(byteArrayOutputStream);
              kryo.writeClassAndObject(output, user);
              output.close();
              genericObjectPool.returnObject(kryo);
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        }
      });
    }
  }
}
